package com.eco.utils;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public class HexDataUtil {
    private static final Logger log = LoggerFactory.getLogger(com.eco.utils.HexDataUtil.class);

    public static String getls2(String ls1) {
        DecimalFormat df = new DecimalFormat("#.000");
        Random random = new Random();
        BigDecimal decimal = BigDecimal.valueOf(Double.valueOf(ls1).doubleValue());
        BigDecimal decimal2 = BigDecimal.valueOf(Double.valueOf(df.format((random.nextDouble() + 0.01D) / 12.0D)).doubleValue());
        return decimal.add(decimal2).toString();
    }

    public static Double getvalue(int value, int valueDecimal) {
        BigDecimal decimal = BigDecimal.valueOf(value);
        BigDecimal base = BigDecimal.valueOf(1L);
        for (int i = 0; i < valueDecimal; i++)
            base = base.multiply(BigDecimal.valueOf(10L));
        BigDecimal divide = decimal.divide(base);
        double v = divide.doubleValue();
        if (v < 0.01D)
            return Double.valueOf(0.0D);
        return Double.valueOf(v);
    }

    public static int byte2IntHighLow(byte[] bytes) {
        return (bytes[0] & 0xFF) << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | bytes[3] & 0xFF;
    }

    public static int byte2IntLowHigh(byte[] bytes) {
        return (bytes[2] & 0xFF) << 24 | (bytes[3] & 0xFF) << 16 | (bytes[0] & 0xFF) << 8 | bytes[1] & 0xFF;
    }

    public static float byte2Float(byte[] bytes) {
        int res = byte2IntHighLow(bytes);
        float ls = Float.intBitsToFloat(res);
        return ls;
    }

    public static int byte2Short(byte[] bytes) {
        return Short.toUnsignedInt(ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getShort());
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0)
            return null;
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2)
                stringBuilder.append(0);
            stringBuilder.append(hv);
            stringBuilder.append(' ');
        }
        return stringBuilder.toString();
    }

    public static String dateNow() {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(fmt);
    }

    public static String date2String(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static boolean isNullAndZero(Integer pageNum) {
        return (StringUtils.isEmpty(pageNum) || pageNum.intValue() < 1);
    }

    public static BigDecimal waterLevelAdd(Double waterLevel) {
        BigDecimal decimal = BigDecimal.valueOf(waterLevel.doubleValue());
        BigDecimal add = decimal.add(BigDecimal.valueOf(0.6D));
        return add;
    }

    public static String getCRC(byte[] bytes) {
        int CRC = 65535;
        int POLYNOMIAL = 40961;
        for (int i = 0; i < bytes.length; i++) {
            CRC ^= bytes[i] & 0xFF;
            for (int j = 0; j < 8; j++) {
                if ((CRC & 0x1) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        return result.substring(2, 4) + result.substring(2, 4);
    }

    public static byte hexToByte(String inHex) {
        return (byte)Integer.parseInt(inHex, 16);
    }

    public static byte[] hexToByteArray(String inHex) {
        byte[] result;
        int hexlen = inHex.length();
        if (hexlen % 2 == 1) {
            hexlen++;
            result = new byte[hexlen / 2];
            inHex = "0" + inHex;
        } else {
            result = new byte[hexlen / 2];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
}
